package com.crosspaste.ui.devices

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.crosspaste.db.sync.SyncRuntimeInfo
import com.crosspaste.platform.Platform
import com.crosspaste.sync.SyncManager
import com.crosspaste.ui.base.DialogButtonsView
import com.crosspaste.ui.base.DialogService
import com.crosspaste.ui.base.PasteDialogFactory

interface DeviceScope : PlatformScope {

    var syncRuntimeInfo: SyncRuntimeInfo

    override val platform: Platform
        get() = syncRuntimeInfo.platform

    override fun getDeviceDisplayName(): String = syncRuntimeInfo.getDeviceDisplayName()

    @Composable
    fun DeviceConnectView()

    fun onEditDevice(
        dialogService: DialogService,
        pasteDialogFactory: PasteDialogFactory,
        syncManager: SyncManager,
    ) {
        dialogService.pushDialog(
            pasteDialogFactory.createDialog(
                key = syncRuntimeInfo.deviceId,
                title = "input_note_name",
            ) {
                var inputNoteName by remember { mutableStateOf(syncRuntimeInfo.noteName ?: "") }
                var isError by remember { mutableStateOf(false) }

                val cancelAction = {
                    dialogService.popDialog()
                }

                val confirmAction = {
                    if (inputNoteName == "") {
                        isError = true
                    } else {
                        syncManager.updateNoteName(syncRuntimeInfo.appInstanceId, inputNoteName)
                        dialogService.popDialog()
                    }
                }

                Column(
                    modifier =
                        Modifier
                            .fillMaxWidth()
                            .wrapContentHeight(),
                ) {
                    Row(modifier = Modifier.fillMaxWidth()) {
                        DeviceNoteEditField(
                            cancelAction,
                            confirmAction,
                            inputNoteName,
                            isError,
                            { inputNoteName = it },
                            syncRuntimeInfo,
                        )
                    }

                    Row(
                        modifier =
                            Modifier
                                .fillMaxWidth()
                                .wrapContentHeight(),
                    ) {
                        DialogButtonsView(
                            cancelAction = cancelAction,
                            confirmAction = confirmAction,
                        )
                    }
                }
            },
        )
    }
}
